package Controller;

import Service.MenuService;
import Service.RoleService;
import domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

        /*
         查询所有父子菜单信息
    */

    @RequestMapping("/findAllMenu") //Get请求没有携带参数 此处不需要写任何参数
    public ResponseResult findAllMenu(){

        // -1 代表查询所有的父子级菜单
        List<Menu> list = menuService.findSubMenuListByPid(-1);


        // 响应数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("parentMenuList",list);
        ResponseResult result = new ResponseResult(true, 200, "查询所有父子菜单查询成功", map);

        return result;

    }

    /*
         查询所有角色信息 + 多条件
     */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role){

        List<Role> roles = roleService.findAllRole(role);
        ResponseResult result = new ResponseResult(true, 200, "角色查询成功", roles);

        return result;

    }

    /*
       根据角色id 查询该角色关联的菜单信息 ---> ID[1,2,3..]
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(int roleId){

        List<Integer> menuByRoleId = roleService.findMenuByRoleId(roleId);

        ResponseResult result = new ResponseResult(true, 200, "该角色关联的菜单信息查询成功", menuByRoleId);

        return result;
    }

    /*
            4.根据当前角色 查询关联的资源信息
     */
    @RequestMapping("/findResourceByRoleId")
    public ResponseResult findResourceByRoleId(int roleId){

        List<String> resourceByRoleId = roleService.findResourceByRoleId(roleId);
        return new ResponseResult(true,200,"该角色关联的资源信息查询成功",resourceByRoleId);
    }

    /*
        为角色分配菜单
     */

    @RequestMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVO roleMenuVO){

        roleService.saveRoleContextMenu(roleMenuVO);


        return new ResponseResult(true,200,"角色分配菜单成功",null);

    }

    /*
            删除角色
     */

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(int roleid){

        roleService.deleteRole(roleid);
        return new ResponseResult(true,200,"用户删除成功",null);
    }






}
